package com.kuaiditu.assistant.module.storage;

import android.content.Context;

import com.kuaiditu.assistant.App;
import com.kuaiditu.assistant.entity.AssistantUser;
import com.kuaiditu.assistant.entity.AssistantUserDao;

/**
 * Created by Eenie on 2017/8/31.
 * Email: eeniework@gmail.com
 * Des:
 */

public class AssistantUserStorage {

    private Context mContext;
    private AssistantUser mUser;
    private AssistantUserDao mAssistantUserDao;

    public AssistantUserStorage(App context) {
        this.mContext = context;
        mAssistantUserDao = context.getApplicationComponent().getDaoSession().getAssistantUserDao();
        mUser = recoverUser();
    }

    private AssistantUser recoverUser() {
        return mAssistantUserDao.queryBuilder()
                .build()
                .unique();
    }


    public AssistantUser getUser() {
        return mUser;
    }

    public void saveUser(AssistantUser user) {
        mAssistantUserDao.deleteAll();
      mAssistantUserDao.insertOrReplace(user);
        mUser = recoverUser();
    }


    public void logout() {
        mAssistantUserDao.deleteAll();
        mUser = null;
    }


    public boolean isLogin() {
        return mUser != null;
    }

    public String getToken() {
        return isLogin() ? String.valueOf(mUser.getCourier_id()) : "-1";
    }

}
